package com.ruoyi.crm.sale.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.crm.sale.domain.CrmContract;
import com.ruoyi.crm.sale.mapper.CrmContractMapper;
import com.ruoyi.crm.sale.service.ICrmBusinessProductService;
import com.ruoyi.crm.sale.service.ICrmContractService;

/**
 * 合同管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-09-18
 */
@Service
public class CrmContractServiceImpl implements ICrmContractService 
{
    @Autowired
    private CrmContractMapper crmContractMapper;
    @Autowired
    private ICrmBusinessProductService crmBusinessProductService;

    /**
     * 查询合同管理
     * 
     * @param id 合同管理ID
     * @return 合同管理
     */
    @Override
    public CrmContract selectCrmContractById(Long id)
    {
        return crmContractMapper.selectCrmContractById(id);
    }

    /**
     * 查询合同管理列表
     * 
     * @param crmContract 合同管理
     * @return 合同管理
     */
    @Override
    @DataScope(deptAlias = "t", userAlias = "d")
    public List<CrmContract> selectCrmContractList(CrmContract crmContract)
    {
        return crmContractMapper.selectCrmContractList(crmContract);
    }

    /**
     * 新增合同管理
     * 
     * @param crmContract 合同管理
     * @return 结果
     */
    @Override
    public int insertCrmContract(CrmContract crmContract)
    {
    	crmContract.setCreateTime(DateUtils.getNowDate());
    	crmContract.setUpdateTime(DateUtils.getNowDate());
    	crmContract.setCreateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
        int status = crmContractMapper.insertCrmContract(crmContract);
        
        //保存产品信息
        crmBusinessProductService.updateCrmContractProduct(crmContract.getBusProductList(), crmContract);
        
        return status;
    }

    /**
     * 修改合同管理
     * 
     * @param crmContract 合同管理
     * @return 结果
     */
    @Override
    public int updateCrmContract(CrmContract crmContract)
    {
    	crmContract.setUpdateTime(DateUtils.getNowDate());
    	crmContract.setUpdateBy(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
//    	crmContract.setCheckStatus(Long.valueOf(0));//修改之后默认改成待审状态
        int status = crmContractMapper.updateCrmContract(crmContract);
        
        //保存产品信息
        crmBusinessProductService.updateCrmContractProduct(crmContract.getBusProductList(), crmContract);
        
        return status;
    }

    /**
     * 合同提交审核
     * 
     * @param crmContract 合同管理
     * @return 结果
     */
    public int submitCheckStatus(CrmContract crmContract) {
    	return crmContractMapper.submitCheckStatus(crmContract);
    }

    /**
     * 批量删除合同管理
     * 
     * @param ids 需要删除的合同管理ID
     * @return 结果
     */
    @Override
    public int deleteCrmContractByIds(Long[] ids)
    {
        return crmContractMapper.deleteCrmContractByIds(ids);
    }

    /**
     * 删除合同管理信息
     * 
     * @param id 合同管理ID
     * @return 结果
     */
    @Override
    public int deleteCrmContractById(Long id)
    {
        return crmContractMapper.deleteCrmContractById(id);
    }
    
    /**
     * 查询合同管理-产品
     * 
     * @param crmContract 合同
     * @return 合同管理
     */
    public CrmContract selectCrmProContractById(CrmContract crmContract) {
    	return crmContractMapper.selectCrmProContractById(crmContract);
    }

    /**
     * 查询已签合同金额
     * 
     * @return 结果
     */
    public double selContractCount(Map<String, Object> params) {
    	return crmContractMapper.selContractCount(params);
    }

    /**
     * 查询未审合同数量
     * 
     * @return 结果
     */
    public int selNoCheckConCount(Map<String, Object> params) {
    	return crmContractMapper.selNoCheckConCount(params);
    }

    /**
     * 查询销售额度统计
     * 
     * @return 结果
     */
    public Map<String, Object> selSaleQuotaCount(Map<String, Object> params){
    	List<Map<String, Object>> totalData = crmContractMapper.selSaleQuotaCount(params);
    	
    	Map<String, Object> resultData = new HashMap<String, Object>();
    	List<String> nameList = new ArrayList<String>();
    	List<Double> valueList = new ArrayList<Double>();
    	for(Map<String, Object> data : totalData) {
    		nameList.add(String.valueOf(data.get("name")));
    		valueList.add(Double.valueOf(data.get("totalMoney").toString()));
    	}
    	
    	resultData.put("names", nameList);
    	resultData.put("values", valueList);
    	resultData.put("totalData", totalData);
    	
    	return resultData;
    }
}
